package com.zhb.controller;


import com.zhb.beans.AjaxResult;
import com.zhb.beans.Page;
import com.zhb.beans.SourcesPhoto;
import com.zhb.service.SourcesPhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: upload_demo
 * @description:
 * @author: rainboz
 * @create: 2019-10-26 15:51
 **/
@Controller
public class PhotoController {

    @Autowired
    private SourcesPhotoService sourcesPhotoService;

    @RequestMapping("/list")
    public String getList(HttpServlet httpServlet){
        Object app_path = httpServlet.getServletContext().getContextPath();
        System.out.println("///////////APP_PATH: "+app_path);
        return "list";
    }
    @ResponseBody
    @RequestMapping("/getPhotoList")
    public Object getPhotoList(String queryText, Integer pageNum, Integer pageSize) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            //分页查询
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("start", (pageNum - 1) * pageSize);
            map.put("size", pageSize);
            map.put("queryText", queryText);

            //获取数据
            List<SourcesPhoto> photoList = sourcesPhotoService.pageQuery(map);
            System.out.println(photoList);

            //总数据条数
            int totalSize =sourcesPhotoService.pageQueryCount(map);

            //最大页码
            int totalNum = 0;
            if (totalSize % pageSize == 0) {
                totalNum = totalSize / pageSize;
            } else {
                totalNum = totalSize / pageSize + 1;
            }
            //分页对象
            Page<SourcesPhoto> photoPage = new Page<SourcesPhoto>();
            photoPage.setDatas(photoList);
            photoPage.setPageNum(pageNum);
            photoPage.setTotalNum(totalNum);
            photoPage.setTotalSize(totalSize);


            ajaxResult.setData(photoPage);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }
}
